package com.jx.gocomtask.webservice.restcontroller;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.jx.gocomtask.webservice.DTO.TodoEditContent;
import com.jx.gocomtask.webservice.DTO.TodoStatus;
import com.jx.gocomtask.webservice.entity.ExtTaskTodo;
import com.jx.gocomtask.webservice.repository.ExtTaskTodoRepository;
import com.jx.gocomtask.webservice.restcontroller.base.BaseRestController;
import com.jx.gocomtask.webservice.service.Route;
import com.jx.gocomtask.webservice.service.RouteExt;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import phoenics.common.BaseData.StatusData;
import phoenics.common.exception.NotFoundException;
import phoenics.common.exception.ParamsException;

@Api(value = "与待办有关的API")
@RestController
@RequestMapping("/todo")
public class TaskTodo extends BaseRestController{
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TaskTodo.class);
	@Autowired
	ExtTaskTodoRepository extTaskTodoRepository;
	
	@Autowired
	Route route;
	@Autowired
	RouteExt routeExt;
	
	@ApiOperation(value = "建立待办", notes = "")  
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public ExtTaskTodo createTodo(
			@RequestParam(value="token",  required=true) String wptoken,
			@RequestBody   ExtTaskTodo extTaskTodo
			){
		return extTaskTodoRepository.save(extTaskTodo);
	}
	@ApiOperation(value = "转G信", notes = "")  
	@RequestMapping(value = "/togxin/{id}", method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public StatusData todo2Gxin(
			@RequestParam(value="token",  required=true) String wptoken,
			@PathVariable("id") long id
			){
		return todo2Gxin(id,false);
	}
	@ApiOperation(value = "转G信完成", notes = "")  
	@RequestMapping(value = "/tofinish/{id}", method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public StatusData todo2GxinInnish(
			@RequestParam(value="token",  required=true) String wptoken,
			@PathVariable("id") long id
			){
		return todo2Gxin(id,true);
	}
	
	private StatusData todo2Gxin(long id ,boolean isfinished){
		ExtTaskTodo extTaskTodo=extTaskTodoRepository.findOne(id);
		if(extTaskTodo==null){
			throw new ParamsException();
		}
		routeExt.saveExtTaskMessage(extTaskTodo,route, isfinished);
		extTaskTodo.setTodoStatus(TodoStatus.TASK);
		extTaskTodoRepository.save(extTaskTodo);
		return new StatusData(0,"ok");
	}
			
	@ApiOperation(value = "待办列表", notes = "")  
	@RequestMapping(value = "/todo/list", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public Page<ExtTaskTodo> todoList(
			HttpServletRequest request,
			@RequestParam(value="token",  required=true) String wptoken,
			@RequestParam(value = "keyword", defaultValue = "" ,required=false) String keyword,
			//@RequestParam(value = "today", defaultValue = "" ,required=false) String today,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size
			){
		String userid=(String)request.getAttribute("userid");
		 Sort sort = new Sort(Direction.DESC, "id");
		 Pageable pageable = new PageRequest(page, size, sort);
		 if(!keyword.equals("")){
			 return extTaskTodoRepository.findByTodoStatusAndFromUserAndContentLike(TodoStatus.START,userid,"%"+keyword+"%" ,pageable);
		 }
		 //if(!today.equals("")){
			// SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd"); 
			// if(formatter.parse(today)!=null)
			// return extTaskTodoRepository.findByTodoStatusAndFromUser(TodoStatus.START, userid,pageable);
		 //}
		 return extTaskTodoRepository.findByTodoStatusAndFromUser(TodoStatus.START, userid,pageable);
	}
	@ApiOperation(value = "待办详情", notes = "")  
	@RequestMapping(value = "/details/{id}", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public ExtTaskTodo details(
			@RequestParam(value="token",  required=true) String wptoken,
			@PathVariable("id") long id
			){
		ExtTaskTodo extTaskTodo=extTaskTodoRepository.findOne(id);
		if(extTaskTodo==null){
			throw new NotFoundException();
		}
		return extTaskTodo;
	}
	@ApiOperation(value = "待办编辑", notes = "")  
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public ExtTaskTodo edit(
			@RequestParam(value="token",  required=true) String wptoken,
			@PathVariable("id") long id,
			@RequestBody   TodoEditContent content
			){
		ExtTaskTodo extTaskTodo=extTaskTodoRepository.findOne(id);
		if(extTaskTodo==null){
			throw new NotFoundException();
		}
		extTaskTodo.setContent(content.getContent());
		return extTaskTodoRepository.save(extTaskTodo);
	}
	@ApiOperation(value = "待办编辑并转G信", notes = "")  
	@RequestMapping(value = "/edit2g/{id}", method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public StatusData edit2g(
			@RequestParam(value="token",  required=true) String wptoken,
			@PathVariable("id") long id,
			@RequestBody   TodoEditContent content
			){
		return edit4g(id,content,false);
	}
	@ApiOperation(value = "待办编辑并转G信完成", notes = "")  
	@RequestMapping(value = "/edit2finish/{id}", method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public StatusData edit2finish(
			@PathVariable("id") long id,
			@RequestBody   TodoEditContent content
			){
		return edit4g(id,content,true);
	}
	private StatusData edit4g(
			long id,
			TodoEditContent content,
			boolean isfinished
			){
		ExtTaskTodo extTaskTodo=extTaskTodoRepository.findOne(id);
		if(extTaskTodo==null){
			throw new NotFoundException();
		}
		extTaskTodo.setContent(content.getContent());
		extTaskTodo.setTodoStatus(TodoStatus.TASK);
		extTaskTodoRepository.save(extTaskTodo);
		routeExt.saveExtTaskMessage(extTaskTodo,route, isfinished);
		return new StatusData(0,"ok");
	}
	@ApiOperation(value = "存档列表", notes = "")  
	@RequestMapping(value = "/backup/list", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public Page<ExtTaskTodo> backupList(
			@RequestParam(value="token",  required=true) String wptoken,
			HttpServletRequest request,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size
			){
		String userid=(String)request.getAttribute("userid");
		 Sort sort = new Sort(Direction.DESC, "id");
		 Pageable pageable = new PageRequest(page, size, sort);
		 return extTaskTodoRepository.findByTodoStatusAndFromUser(TodoStatus.BACKUP,userid, pageable);
	}
	@ApiOperation(value = "一览", notes = "")  
	@RequestMapping(value = "/all/list", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public Page<ExtTaskTodo> allList(
			@RequestParam(value="token",  required=true) String wptoken,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size
			){
		
		 Sort sort = new Sort(Direction.DESC, "id");
		 Pageable pageable = new PageRequest(page, size, sort);
		 return extTaskTodoRepository.findAll( pageable);
	}
	@ApiOperation(value = "一键存档", notes = "")  
	@RequestMapping(value = "/tobackup", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	public Page<ExtTaskTodo> tobackup(
			HttpServletRequest request,
			@RequestParam(value="token",  required=true) String wptoken,
			@RequestParam(value = "size", defaultValue = "10") Integer size
			){
		String userid=(String)request.getAttribute("userid");
		 Sort sort = new Sort(Direction.DESC, "id");
		 Pageable pageable = new PageRequest(0, size, sort);
		 extTaskTodoRepository.updateTodoStatus2back(userid);
		 return extTaskTodoRepository.findByTodoStatusAndFromUser(TodoStatus.BACKUP,userid, pageable);
	}
}
